package learn.netty.server.handler;

import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpRequestDecoder;
import io.netty.handler.codec.http.HttpResponseEncoder;
import io.netty.handler.codec.http.HttpServerCodec;
import io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler;

/**
 * @USER white
 * @DATETIME 2020/10/17 11:33
 */
public class ServerInitializeHandler extends ChannelInitializer<SocketChannel> {


    @Override
    protected void initChannel(SocketChannel ch) throws Exception {
        ChannelPipeline pipeline = ch.pipeline();
        //处理http消息的编解码
        pipeline.addLast("httpServerCodec", new HttpServerCodec());
        //等效于
//        pipeline.addLast("httpResponseEndcoder", new HttpResponseEncoder());
//        pipeline.addLast("HttpRequestDecoder", new HttpRequestDecoder());
        pipeline.addLast(new HttpObjectAggregator(1024*10));
        pipeline.addLast(new WebSocketServerProtocolHandler("/"));
        //添加自定义的ChannelHandler
        pipeline.addLast( new HttpServerHandler());
    }
}
